/**
 * 页面加载完成后执行
 */
$(document).ready(function(){
    //初始化城市下拉列表
    $.ajax({
        url:"http://localhost:8080/SunshineAirlines/getCityNames",//路由,接口
        data:"",//需要的传的参数
        type:"post",//传送方式,post,或get,由于登录的密码需要保密,使用post
        success:function(msg){//成功的消息回复
            //1.    将msg转换成JSON格式 的对象
            var json = JSON.parse(msg);
            //2.    判断是否登录成功
            if(json.flag == "success"){//获取obj对象中的flag值
               //1.循环遍历列表数据
               var optionHtml = "";
               for(var i=0;i<json.data.length;i++){
                   //2.拼接option HTML代码字符串
                   optionHtml+="<option value='"+json.data[i].CityName+"'>"+json.data[i].CityName+"</option>";
               }
               //3.添加到下拉列表框元素中
               $(".fromCity").html(optionHtml);
               $(".toCity").html(optionHtml);
            }else{
                //$(".alertInfo").text(json.data);
            }
        }
   });

   $(".searchFlight").click(function(){
       //1.判断是双程还是单程
        if($(".oneWay").prop("checked")){
            //单程
            //var optionone = $(".optionone").val();

            var fromCity = $(".fromCity").val();
            var toCity = $(".toCity").val();
            var departureDate = $(".departureDate").val();
            //var returnDate = $(".returnDate").val();
            var cabinType = $(".cabinType").val();
            var flightType = $(".flightType").val();
            var params ="fromCity="+fromCity+"&toCity="+toCity+"&departureDate="+departureDate+
            "&cabinTypeId="+cabinType+"&flightType="+flightType;
            $.ajax({
                url:"http://localhost:8080/SunshineAirlines/getSearchFlight",
                data:params,
                type:"post",
                success:function(msg){
                    var json = JSON.parse(msg);
                    if(json.flag == "success"){
                      var flightHtml = "";
                      //循环遍历数据拼接HTML代码字符串
                      for(var i=0;i<json.data.length;i++){
                          if(json.data[i].FlightType == "Non-stop"){
                              var price =  "";
                              var cabinTypeName = "";
                                if(cabinType == 3){
                                    cabinTypeName = "First";
                                    price = (json.data[i].EconomyPrice*1.5).toFixed(2);
                                }else if(cabinType == 2){
                                    cabinTypeName = "Business";
                                    price = (json.data[i].EconomyPrice*1.25).toFixed(2);
                                }else{
                                    cabinTypeName = "Economy";
                                    price = (json.data[i].EconomyPrice).toFixed(2);
                                }

                                var ontimeRate = ((json.data[i].NotDelay/json.data[i].AllCount)*100).toFixed(2);
                              //无中转航班是含有sheduleId字段
                              var dateStr=json.data[i].Date.substring(0,16);
                              var preArrivalTime=json.data[i].Date.substring(0,16);
                              var flightTimeHour =  parseInt(json.data[i].FlightTime/60);
                              var flightTimeMinute = json.data[i].FlightTime%60;
                              var flightTimeStr ="";
                              if(flightTimeHour>0){
                                flightTimeStr+=flightTimeHour+" h ";
                              }
                              if(flightTimeMinute>0){
                                  flightTimeStr+=flightTimeMinute+" m";
                              }

                            flightHtml+="<div class='innermsg'>"+
                                                "<div class='innerlist'>"+
                                                    "<p>$"+price+"</p>"+
                                                    "<p>"+cabinTypeName+"</p>"+
                                                    "<p>Flight "+json.data[i].FlightNumber+"("+ontimeRate+"%)</p>"+
                                                "</div>"+
                                                "<div class='innerlist' style='width: 450px;'>"+
                                                    "<div class='placelist'> "+
                                                            "<p class='citymsg'>"+json.data[i].DepartCityName+"/"+json.data[i].DepartureAirportIATA+"</p>"+
                                                            "<p class='datemsg'>"+dateStr+"</p>"+
                                                        "</div>"+
                                                    "<div class='placelist'> "+
                                                    "<div class='citymsg'>"+json.data[i].ArriveCityName+"/"+json.data[i].ArrivalAirportIATA+"</div>"+
                                                        "<div class='datemsg'>"+preArrivalTime+"</div> "+
                                                        "</div>"+
                                                    "</div>"+
                                                "<div class='innerlist'>"+
                                                    "<p>Non-stop </p>"+
                                                    //"<p>Total time:10h 0m</p>"+
                                                    "<p>Total time:"+flightTimeStr+"</p>"+
                                                    "<p style='color: red;'>"+json.data[i].ResidueTickets+" available tickets</p>"+   
                                                    "</div>"+
                                            "</div>"
                          }else{

                          }
                      }
                      //HTMl加载到元素中
                      $(".msglist").html(flightHtml);
                    }else{
                        //$(".alertInfo").text(json.data);
                    }
                }
           });
        }else{
            //往返双程

        }
   })
});




